package o;

import android.text.TextUtils;
import com.huawei.appmarket.framework.startevents.control.TabIconCache;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import o.fus.a;

public class dhy implements dmi {
    private List<ckm> ˊ;
    private LinkedHashMap<Integer, boolean[]> ˋ = new LinkedHashMap();
    private final dhx ˎ;
    private final int ˏ;

    public dhy(List<ckm> list, int i, dhx o_dhx) {
        if (!CollectCheck.isEmpty((List) list)) {
            this.ˊ = new ArrayList(list);
        }
        this.ˏ = i;
        this.ˎ = o_dhx;
    }

    public void run() {
        Exception e;
        if (CollectCheck.isEmpty(this.ˊ)) {
            Logger.ˎ("GetTabIconTask", "GetTabIconTask: tabIcon is empty.");
            dik.ˏ(this.ˏ);
            return;
        }
        String ˋ = dik.ˋ(this.ˏ);
        if (ˋ == null) {
            Logger.e("GetTabIconTask", "GetTabIconTask: rootPath is null.");
            return;
        }
        dik.ˏ(this.ˊ.size(), this.ˏ);
        this.ˋ.clear();
        boolean z = false;
        int i = 0;
        for (ckm o_ckm : this.ˊ) {
            boolean ˋ2;
            try {
                this.ˋ.put(Integer.valueOf(i), new boolean[2]);
                ˋ2 = ˋ(o_ckm, ˋ, "tabIconNormal", "tabIconClicked", i);
                if (ˋ2) {
                    continue;
                    i++;
                    z = ˋ2;
                } else {
                    try {
                        this.ˎ.ॱ(false, this.ˋ);
                        return;
                    } catch (Exception e2) {
                        e = e2;
                    }
                }
            } catch (Exception e3) {
                e = e3;
                ˋ2 = z;
                Logger.ˋ("GetTabIconTask", "downloadTabIcon error : " + e.getMessage());
                i++;
                z = ˋ2;
            }
        }
        if (z) {
            this.ˎ.ॱ(true, this.ˋ);
        }
    }

    private boolean ˋ(String str, OutputStream outputStream) {
        fvb execute;
        InputStream bufferedInputStream;
        OutputStream bufferedOutputStream;
        Throwable th;
        Exception e;
        fvb o_fvb;
        Throwable th2;
        OutputStream outputStream2 = null;
        try {
            fuv ˊ = dnb.ˊ();
            a ˎ = new a().ˎ(str);
            ˎ.ˋ("Accept-Encoding", "identity");
            execute = ˊ.ˎ(ˎ.ॱ()).execute();
            try {
                if (execute.ˏ()) {
                    fuy ᐝ = execute.ᐝ();
                    long ˏ = ᐝ.ˏ();
                    if (0 == ˏ) {
                        Logger.e("GetTabIconTask", "downloadUrlToStream error, body.contentLength() is 0.");
                        ˏ(execute, null, null);
                        return false;
                    }
                    try {
                        bufferedInputStream = new BufferedInputStream(ᐝ.ˎ(), 8192);
                        try {
                            bufferedOutputStream = new BufferedOutputStream(outputStream, 8192);
                        } catch (Throwable th3) {
                            th = th3;
                            bufferedOutputStream = null;
                            ˏ(execute, bufferedInputStream, bufferedOutputStream);
                            throw th;
                        }
                        try {
                            byte[] bArr = new byte[8192];
                            int i = 0;
                            while (true) {
                                int read = bufferedInputStream.read(bArr);
                                if (read != -1) {
                                    bufferedOutputStream.write(bArr, 0, read);
                                    i += read;
                                } else {
                                    try {
                                        break;
                                    } catch (Exception e2) {
                                        e = e2;
                                        o_fvb = execute;
                                        try {
                                            Logger.ˋ("GetTabIconTask", "downloadUrlToStream error: " + e.toString());
                                            ˏ(o_fvb, bufferedInputStream, bufferedOutputStream);
                                            return false;
                                        } catch (Throwable th4) {
                                            th2 = th4;
                                            execute = o_fvb;
                                            outputStream2 = bufferedOutputStream;
                                            ˏ(execute, bufferedInputStream, outputStream2);
                                            throw th2;
                                        }
                                    } catch (Throwable th5) {
                                        th2 = th5;
                                        outputStream2 = bufferedOutputStream;
                                        ˏ(execute, bufferedInputStream, outputStream2);
                                        throw th2;
                                    }
                                }
                            }
                            ˏ(execute, bufferedInputStream, bufferedOutputStream);
                            boolean z = ˏ == ((long) i);
                            if (!z) {
                                Logger.e("GetTabIconTask", "downloadUrlToStream error, body.contentLength() != total.");
                            }
                            ˏ(execute, null, null);
                            return z;
                        } catch (Throwable th6) {
                            th = th6;
                            ˏ(execute, bufferedInputStream, bufferedOutputStream);
                            throw th;
                        }
                    } catch (Throwable th7) {
                        th = th7;
                        bufferedOutputStream = null;
                        bufferedInputStream = null;
                        ˏ(execute, bufferedInputStream, bufferedOutputStream);
                        throw th;
                    }
                } else {
                    Logger.e("GetTabIconTask", "downloadUrlToStream error, responseCode: " + execute.ˊ());
                    ˏ(execute, null, null);
                    return false;
                }
            } catch (Exception e3) {
                e = e3;
                bufferedOutputStream = null;
                bufferedInputStream = null;
                o_fvb = execute;
            } catch (Throwable th8) {
                th2 = th8;
                bufferedInputStream = null;
                ˏ(execute, bufferedInputStream, outputStream2);
                throw th2;
            }
        } catch (Exception e4) {
            e = e4;
            bufferedOutputStream = null;
            bufferedInputStream = null;
            Logger.ˋ("GetTabIconTask", "downloadUrlToStream error: " + e.toString());
            ˏ(o_fvb, bufferedInputStream, bufferedOutputStream);
            return false;
        } catch (Throwable th9) {
            th2 = th9;
            execute = null;
            bufferedInputStream = null;
            ˏ(execute, bufferedInputStream, outputStream2);
            throw th2;
        }
    }

    private void ˏ(fvb o_fvb, InputStream inputStream, OutputStream outputStream) {
        dnw.ˎ(inputStream);
        dnw.ˎ(outputStream);
        dnw.ˎ(o_fvb);
    }

    private String ॱ(String str, String str2, String str3) {
        return str3 + (str2 + str.substring(str.lastIndexOf("/") + 1, str.length()));
    }

    private boolean ˋ(ckm o_ckm, String str, String str2, String str3, int i) {
        if (o_ckm == null) {
            Logger.e("GetTabIconTask", "downloadTabIcon: the column is null");
            return false;
        } else if (TextUtils.isEmpty(o_ckm.ˏ()) || TextUtils.isEmpty(o_ckm.ˎ())) {
            Logger.e("GetTabIconTask", "downloadTabIcon: tabIcon=" + o_ckm.ˏ() + ", tabIconClicked=" + o_ckm.ˎ());
            return false;
        } else {
            String ॱ = ॱ(o_ckm.ˏ(), str2, str);
            String ॱ2 = ॱ(o_ckm.ˎ(), str3, str);
            boolean ˏ = ˏ(ॱ, o_ckm.ˏ(), i, str2);
            boolean ˏ2 = ˏ(ॱ2, o_ckm.ˎ(), i, str3);
            if (ˏ && ˏ2) {
                TabIconCache ॱ3 = dik.ॱ(this.ˏ, i);
                if (ॱ3 == null) {
                    ॱ3 = new TabIconCache();
                }
                ॱ3.ˏ(ॱ);
                ॱ3.ˊ(ॱ2);
                ॱ3.ˎ(o_ckm.ˊ());
                dik.ˊ(ॱ3, i, this.ˏ);
            }
            boolean z = ˏ && ˏ2;
            return z;
        }
    }

    private boolean ˏ(String str, String str2, int i, String str3) {
        Closeable fileOutputStream;
        Closeable closeable;
        Throwable th;
        Exception e;
        boolean[] zArr = (boolean[]) this.ˋ.get(Integer.valueOf(i));
        if (zArr == null || 2 != zArr.length) {
            Logger.e("GetTabIconTask", "downloadImage: fileIsRefreshed is null or length is error.");
            return false;
        }
        File file = new File(str);
        if (file.exists() && 0 != file.length()) {
            return ˋ(i, str3, zArr, true);
        }
        Logger.ˎ("GetTabIconTask", "downloadImage: " + str2);
        try {
            fileOutputStream = new FileOutputStream(file);
            try {
                if (ˋ(str2, fileOutputStream)) {
                    boolean ˋ = ˋ(i, str3, zArr, true);
                    dnw.ˎ(fileOutputStream);
                    return ˋ;
                }
                Logger.ˋ("GetTabIconTask", "download tab icon image failed");
                if (file.exists() && !file.delete()) {
                    Logger.ˋ("GetTabIconTask", "file delete error.");
                }
                dnw.ˎ(fileOutputStream);
                return false;
            } catch (FileNotFoundException e2) {
                closeable = fileOutputStream;
                try {
                    Logger.e("GetTabIconTask", "download tab icon image FileNotFoundException error:");
                    dnw.ˎ(closeable);
                    return false;
                } catch (Throwable th2) {
                    fileOutputStream = closeable;
                    th = th2;
                    dnw.ˎ(fileOutputStream);
                    throw th;
                }
            } catch (Exception e3) {
                e = e3;
                try {
                    Logger.e("GetTabIconTask", "download tab icon image Exception error:" + e.toString());
                    dnw.ˎ(fileOutputStream);
                    return false;
                } catch (Throwable th3) {
                    th = th3;
                    dnw.ˎ(fileOutputStream);
                    throw th;
                }
            }
        } catch (FileNotFoundException e4) {
            closeable = null;
            Logger.e("GetTabIconTask", "download tab icon image FileNotFoundException error:");
            dnw.ˎ(closeable);
            return false;
        } catch (Exception e5) {
            e = e5;
            fileOutputStream = null;
            Logger.e("GetTabIconTask", "download tab icon image Exception error:" + e.toString());
            dnw.ˎ(fileOutputStream);
            return false;
        } catch (Throwable th4) {
            th = th4;
            fileOutputStream = null;
            dnw.ˎ(fileOutputStream);
            throw th;
        }
    }

    private boolean ˋ(int i, String str, boolean[] zArr, boolean z) {
        if ("tabIconNormal".equals(str)) {
            zArr[0] = z;
        } else if ("tabIconClicked".equals(str)) {
            zArr[1] = z;
        }
        this.ˋ.put(Integer.valueOf(i), zArr);
        return true;
    }
}
